﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.DAL
{
    public class SupplierDAL:ISupplierDAL
    {

        #region ISupplierDAL 成员

        public IDataReader Select(int SupplierNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Reader(ConnectionString.ConnString, "Proc_Supplier_Select", new SqlParameter[] { new SqlParameter("@SupplierNum", SupplierNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }    
        }

        public IDataReader Select(string SupplierName)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Reader(ConnectionString.ConnString, "Proc_Supplier_Select_By_Name", new SqlParameter[] { new SqlParameter("@SupplierName", SupplierName) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message); 
                throw;
            }
        }

        public DataTable Select()
        {
            try
            {
                return SqlDbHelper.RunSQL_DataTable(ConnectionString.ConnString, "Proc_Supplier_Select_All");
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }


        //public int Insert(string SupplierName, string SupplierCode, string Address, string ContactPerson, string Telephone, string Email, string CreditLevel, int FlagDisable)

        public int Insert( string SupplierName, string SupplierCode, string Address, string ContactPerson, string Telephone, string Email, string CreditLevel, int FlagDisable)

        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@SupplierName",SupplierName),
                    new SqlParameter("@SupplierCode",SupplierCode),
                    new SqlParameter("@Address",Address),
                    new SqlParameter("@ContactPerson",ContactPerson),
                    new SqlParameter("@Telephone",Telephone),
                    new SqlParameter("@Email",Email),
                    new SqlParameter("@CreditLevel",CreditLevel),
                    new SqlParameter("@FlagDisable",FlagDisable)
                };

                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Supplier_Insert", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }           
        }

        public int Update(int SupplierNum, string SupplierName, string SupplierCode, string Address, string ContactPerson, string Telephone, string Email, string CreditLevel, int FlagDisable)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@SupplierNum",SupplierNum),
                    new SqlParameter("@SupplierName",SupplierName),
                    new SqlParameter("@SupplierCode",SupplierCode),
                    new SqlParameter("@Address",Address),
                    new SqlParameter("@ContactPerson",ContactPerson),
                    new SqlParameter("@Telephone",Telephone),
                    new SqlParameter("@Email",Email),
                    new SqlParameter("@CreditLevel",CreditLevel),
                    new SqlParameter("@FlagDisable",FlagDisable)
                };

                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Supplier_Update", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }      
        }

        public int Delete(int SupplierNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Supplier_Delete", new SqlParameter[] { new SqlParameter("@SupplierNum", SupplierNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }          
        }

        #endregion
    }
}
